package com.zuipin.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.BaseDictionaryData;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IBaseDictionaryDataService;
import com.zuipin.util.Pagination;
import com.zuipin.util.RegexUtils;
import com.zuipin.vo.BaseDictionaryDataVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 字典数据值Controller
 * 
 * @ClassName: BaseDictionaryDataController
 * @Description: TODO
 * @author robot
 * @date 2017年04月247日 16:56:13
 */
@Api(value = "基础数据-chenjunjie", description = "基础数据-chenjunjie")
@RestController
@RequestMapping(value = "/data")
public class BaseDictionaryDataController {
	@Resource
	public IBaseDictionaryDataService	baseDictionaryDataService;
	Logger								log	= LoggerFactory.getLogger(getClass());
	
	// @RequestMapping(value = "/get", method = RequestMethod.GET)
	// @ApiOperation(value = "根据主键查询字典数据值", notes = "查询字典数据值")
	// public Result<BaseDictionaryData> getBaseDictionaryDataById(@ApiParam(required = true, name = "id", value = "主键 序列自增") @RequestParam Long id){
	// BaseDictionaryData baseDictionaryData = baseDictionaryDataService.findById(id);
	// Result<BaseDictionaryData> result = new Result<BaseDictionaryData>();
	// if (baseDictionaryData != null) {
	// result.setData(baseDictionaryData);
	// result.setErrorCode(ErrorCode.SUCCESS);
	// } else {
	// result.setErrorCode(ErrorCode.ELSE);
	// }
	// return result;
	// }
	//
	// @RequestMapping(value = "/insert", method = RequestMethod.POST)
	// @ApiOperation(value = "新增字典数据值", notes = "新增字典数据值")
	// @Valids({
	//
	// @Valid(name = "baseDictionaryData.dataSimpleName", maxLength = 50),
	// @Valid(name = "baseDictionaryData.dataName", maxLength = 100),
	// @Valid(name = "baseDictionaryData.dataCode", maxLength = 50),
	// @Valid(name = "baseDictionaryData.isDefault", maxLength = 3),
	// @Valid(name = "baseDictionaryData.isActive", maxLength = 3),
	// @Valid(name = "baseDictionaryData.dictionaryId", maxLength = 19),
	// @Valid(name = "baseDictionaryData.dataIconUrl", maxLength = 120),
	// @Valid(name = "baseDictionaryData.backColor", maxLength = 10),
	// @Valid(name = "baseDictionaryData.sort", maxLength = 10),
	// @Valid(name = "baseDictionaryData.sysId", maxLength = 10),
	// })
	// @ApiImplicitParams({
	//
	// @ApiImplicitParam(name = "baseDictionaryData.dataSimpleName", value = "字典值简称", required = true, dataType = "dataSimpleName", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataName", value = "字典值名称", required = true, dataType = "dataName", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataCode", value = "字典值编码", required = true, dataType = "dataCode", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.isDefault", value = "是否为默认值 0:false 1:true", required = true, dataType = "isDefault", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.isActive", value = "是否冻结 0:禁用 1:激活", required = true, dataType = "isActive", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dictionaryId", value = "字典ID 关联dictionary表主键", required = true, dataType = "dictionaryId", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataIconUrl", value = "字典值图标链接地址", dataType = "dataIconUrl", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.backColor", value = "背景色", dataType = "backColor", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.sort", value = "排序号", required = true, dataType = "sort", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.sysId", value = "平台ID", dataType = "sysId", paramType = "query"),
	// })
	// public Result<BaseDictionaryData> insertBaseDictionaryData(@Inject @ApiIgnore BaseDictionaryData baseDictionaryData) {
	// Result<BaseDictionaryData> result = new Result<BaseDictionaryData>();
	// baseDictionaryDataService.insert(baseDictionaryData);
	// result.setData(baseDictionaryData);
	// result.setErrorCode(ErrorCode.SUCCESS);
	// return result;
	// }
	//
	// @RequestMapping(value = "/update", method = RequestMethod.POST)
	// @ApiOperation(value = "根据主键修改字典数据值", notes = "修改字典数据值")
	// @Valids({
	// @Valid(name = "baseDictionaryData.id", maxLength = 19),
	// @Valid(name = "baseDictionaryData.dataSimpleName", maxLength = 50),
	// @Valid(name = "baseDictionaryData.dataName", maxLength = 100),
	// @Valid(name = "baseDictionaryData.dataCode", maxLength = 50),
	// @Valid(name = "baseDictionaryData.isDefault", maxLength = 3),
	// @Valid(name = "baseDictionaryData.isActive", maxLength = 3),
	// @Valid(name = "baseDictionaryData.dictionaryId", maxLength = 19),
	// @Valid(name = "baseDictionaryData.dataIconUrl", maxLength = 120),
	// @Valid(name = "baseDictionaryData.backColor", maxLength = 10),
	// @Valid(name = "baseDictionaryData.sort", maxLength = 10),
	// @Valid(name = "baseDictionaryData.sysId", maxLength = 10),
	// })
	// @ApiImplicitParams({
	// @ApiImplicitParam(name = "baseDictionaryData.id", value = "主键 序列自增", required = true, dataType = "id", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataSimpleName", value = "字典值简称", required = true, dataType = "dataSimpleName", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataName", value = "字典值名称", required = true, dataType = "dataName", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataCode", value = "字典值编码", required = true, dataType = "dataCode", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.isDefault", value = "是否为默认值 0:false 1:true", required = true, dataType = "isDefault", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.isActive", value = "是否冻结 0:禁用 1:激活", required = true, dataType = "isActive", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dictionaryId", value = "字典ID 关联dictionary表主键", required = true, dataType = "dictionaryId", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataIconUrl", value = "字典值图标链接地址", dataType = "dataIconUrl", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.backColor", value = "背景色", dataType = "backColor", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.sort", value = "排序号", required = true, dataType = "sort", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.sysId", value = "平台ID", dataType = "sysId", paramType = "query"),
	// })
	// public Result<BaseDictionaryData> updateBaseDictionaryData(@Inject @ApiIgnore BaseDictionaryData baseDictionaryData) {
	// Result<BaseDictionaryData> result = new Result<BaseDictionaryData>();
	// baseDictionaryDataService.update(baseDictionaryData);
	// result.setErrorCode(ErrorCode.SUCCESS);
	// return result;
	// }
	//
	// @RequestMapping(value = "/del", method = RequestMethod.DELETE)
	// @ApiOperation(value = "根据主键删除字典数据值", notes = "删除字典数据值")
	// public Result<BaseDictionaryData> delBaseDictionaryData( Long id) {
	// Result<BaseDictionaryData> result = new Result<BaseDictionaryData>();
	// baseDictionaryDataService.delete(id);
	// result.setErrorCode(ErrorCode.SUCCESS);
	// return result;
	// }
	//
	// @RequestMapping(value = "/seach", method = RequestMethod.POST)
	// @ApiOperation(value = "搜索字典数据值SKU字典数据值并分页", notes = "搜索字典数据值并分页")
	// @Valids({
	//
	// @Valid(name = "baseDictionaryData.dataSimpleName", maxLength = 50),
	// @Valid(name = "baseDictionaryData.dataName", maxLength = 100),
	// @Valid(name = "baseDictionaryData.dataCode", maxLength = 50),
	// @Valid(name = "baseDictionaryData.isDefault", maxLength = 3),
	// @Valid(name = "baseDictionaryData.isActive", maxLength = 3),
	// @Valid(name = "baseDictionaryData.dictionaryId", maxLength = 19),
	// @Valid(name = "baseDictionaryData.dataIconUrl", maxLength = 120),
	// @Valid(name = "baseDictionaryData.backColor", maxLength = 10),
	// @Valid(name = "baseDictionaryData.sort", maxLength = 10),
	// @Valid(name = "baseDictionaryData.sysId", maxLength = 10),
	// @Valid(name = "page.pageNumber", maxValue = 999),
	// @Valid(name = "page.pageSize", maxValue = 999),
	// })
	// @ApiImplicitParams({
	//
	// @ApiImplicitParam(name = "baseDictionaryData.dataSimpleName", value = "字典值简称", dataType = "dataSimpleName", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataName", value = "字典值名称", dataType = "dataName", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataCode", value = "字典值编码", dataType = "dataCode", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.isDefault", value = "是否为默认值 0:false 1:true", dataType = "isDefault", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.isActive", value = "是否冻结 0:禁用 1:激活", dataType = "isActive", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dictionaryId", value = "字典ID 关联dictionary表主键", dataType = "dictionaryId", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.dataIconUrl", value = "字典值图标链接地址", dataType = "dataIconUrl", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.backColor", value = "背景色", dataType = "backColor", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.sort", value = "排序号", dataType = "sort", paramType = "query"),
	// @ApiImplicitParam(name = "baseDictionaryData.sysId", value = "平台ID", dataType = "sysId", paramType = "query"),
	// @ApiImplicitParam(name = "page.pageNumber", value = "页码", dataType = "integer", paramType = "query"),
	// @ApiImplicitParam(name = "page.pageSize", value = "单页记录数", dataType = "integer", paramType = "query"),
	// })
	// public Result<List<BaseDictionaryData>> seachBaseDictionaryData(@Inject @ApiIgnore BaseDictionaryData baseDictionaryData,
	// @Inject @ApiIgnore Pagination page) {
	// PageResult<List<BaseDictionaryData>> result = new PageResult<List<BaseDictionaryData>>();
	// result.setData(baseDictionaryDataService.seachBaseDictionaryData(baseDictionaryData, page));
	// result.setTotal_record(page.getTotalRecord());
	// result.setErrorCode(ErrorCode.SUCCESS);
	// return result;
	// }
	
	@RequestMapping(value = "/getDataByType", method = RequestMethod.GET)
	@ApiOperation(value = "按数据类型查询基础数据-chenjunjie", notes = "按数据类型查询基础数据-chenjunjie")
	@Valids({ @Valid(name = "dictionaryId", required = true) })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "dictionaryId", value = "基础数据类型,1:支付方式,2:配送方式,3:商品标签,4:发票类型,5:平台,6:设备,7:在线支付-方式,8:价格类型,9:商品运营方式,10:支付状态,11:第三方帐号平台,12:线下支付-方式", required = true, dataType = "long", paramType = "query") })
	public Result<List<BaseDictionaryDataVo>> getDataByType(@RequestParam Long dictionaryId) {
		PageResult<List<BaseDictionaryDataVo>> result = new PageResult<List<BaseDictionaryDataVo>>();
		BaseDictionaryData baseDictionaryData = new BaseDictionaryData();
		baseDictionaryData.setDictionaryId(dictionaryId);
		List<BaseDictionaryDataVo> datas = baseDictionaryDataService.seachBaseDictionaryData(baseDictionaryData, new Pagination());
		result.setData(datas);
		result.setTotal_record(datas == null ? 0 : datas.size());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/codingInstructions", method = RequestMethod.GET)
	@ApiOperation(value = "查询errorCode说明-chenjunjie", notes = "查询errorCode说明-chenjunjie")
	@Valids({ @Valid(name = "code", required = true, regex = RegexUtils.INTEGER) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "code", value = "errorCode,只能输入数字", required = true, dataType = "long", paramType = "query") })
	public Map<String, Object> codingInstructions(@RequestParam Integer code) {
		ErrorCode errorCode = ErrorCode.getErrorCodeByCode(code);
		Map<String, Object> result = new HashMap<String, Object>();
		if (errorCode == null) {
			result.put("message", "Can not find errorCode " + code);
			result.put("code", code);
			result.put("success", "false");
		} else {
			result.put("message", errorCode.getMessage());
			result.put("code", code);
			result.put("success", "true");
		}
		return result;
	}
	
	@RequestMapping(value = "/errorCodeExplain", method = RequestMethod.GET)
	@ApiOperation(value = "查询所有errorCode与说明-chenjunjie", notes = "查询所有errorCode与说明-chenjunjie")
	public Result<List<Map<String, Object>>> errorCodeExplain() {
		List<ErrorCode> errorCodes = Arrays.asList(ErrorCode.values());
		List<Map<String, Object>> errorCodeList = new ArrayList<>();
		for (ErrorCode error : errorCodes) {
			Map<String, Object> map = new HashMap<>();
			map.put("errorName", error.toString());
			map.put("errorCode", error.getCode());
			map.put("errorMessage", error.getMessage());
			map.put("errorCnMessage", error.getCnMessage());
			errorCodeList.add(map);
		}
		PageResult<List<Map<String, Object>>> result = new PageResult<List<Map<String, Object>>>();
		result.setData(errorCodeList);
		result.setTotal_record(errorCodeList.size());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
}
